<?php

function updateBatch($multipleData = array(), $tableName = "")
{
    $tableName = config('database.connections.mysql.prefix').$tableName;//表前缀

    if ($tableName && !empty($multipleData)) {
        // column or fields to update
        $updateColumn = array_keys($multipleData[0]);
        $referenceColumn = $updateColumn[0]; //e.g id
        unset($updateColumn[0]);
        $whereIn = "";

        $q = "UPDATE `" . $tableName . "` SET `";
        foreach ($updateColumn as $uColumn) {
            $q .= $uColumn . "` = CASE ";

            foreach ($multipleData as $data) {
                $q .= "WHEN " . $referenceColumn . " = " . $data[$referenceColumn] . " THEN '" . $data[$uColumn] . "' ";
            }
            $q .= "ELSE `" . $uColumn . "` END, ";
        }
        foreach ($multipleData as $data) {
            $whereIn .= "'" . $data[$referenceColumn] . "', ";
        }
        $q = rtrim($q, ", ") . " WHERE " . $referenceColumn . " IN (" . rtrim($whereIn, ', ') . ")";
        // Update
        return $q;

    } else {
        return false;
    }
}